package ext.tianma.part.forms;

import java.util.Hashtable;

import ext.com.iba.IBAUtil;
import ext.tianma.bom.util.ClassificationUtil;
import ext.tianma.bom.util.StringUtil;
import wt.csm.navigation.ClassificationNode;
import wt.fc.PersistenceHelper;
import wt.part.WTPart;
import wt.util.WTException;

/**
 * 将部件的分类名称作为部件的中英文名称
 * @author laichao_sun
 *
 */
public class ClassificationNameUntil {
	
	/**
	 * 为原材料中英文名称赋值
	 * @param part
	 * @throws WTException
	 */
	public static void setRawMaterial(WTPart part) throws WTException {
		
		ClassificationNode classificationNode = ClassificationUtil.getClassificationNode(part);
		String name =classificationNode.getName();
		System.out.println("分类名称:"+name);
		if(StringUtil.isNullOrEmpty(name)) {
			throw new WTException("没有获取到分类名称!");
		}
		Hashtable<String, String> values = new Hashtable<String, String>();
		values = addToHashtable(values, "partCNAndENName",name);
		IBAUtil.setSoftTypeIBAValues(part, values);
		//PersistenceHelper.manager.refresh(part);
	}
	/**
	 * 为半成品分类赋值
	 * @param part
	 * @throws WTException
	 */
	public static void setSemiFinished(WTPart part) throws WTException {
		
		ClassificationNode classificationNode = ClassificationUtil.getClassificationNode(part);
		String name =classificationNode.getName();
		System.out.println("分类名称:"+name);
		if(StringUtil.isNullOrEmpty(name)) {
			throw new WTException("没有获取到分类名称!");
		}
		Hashtable<String, String> values = new Hashtable<String, String>();
		values = addToHashtable(values, "SemiFinishedGoodType",name);
		IBAUtil.setSoftTypeIBAValues(part, values);
		//PersistenceHelper.manager.refresh(part);
	}
	
	/**
	 * 实现Hashtable添加数据时不会出现空指针异常
	 * 
	 * @param hashtable
	 * @param key
	 * @param value
	 * @return
	 */
	private static Hashtable<String, String> addToHashtable(Hashtable<String, String> hashtable, String key,
			String value) {
		if (key == null && hashtable != null) {
			return hashtable;
		}

		if (value == null && hashtable != null) {
			return hashtable;
		}

		if (key == null && hashtable == null) {
			return new Hashtable<String, String>();
		}

		if (value == null && hashtable == null) {
			return new Hashtable<String, String>();
		}

		if (hashtable == null) {
			hashtable = new Hashtable<String, String>();
		}
		hashtable.put(key, value);

		return hashtable;
	}

}
